package com.aron;

import com.utils.Audio;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Random;

/**
 * @version v1.0
 * @ProjectName: studycoding
 * @ClassName: Tank
 * @Description: TODO(敌人 ( NPC)坦克实体)
 * @Author: Leo
 * @Date: 2020/6/1 11:39
 */
public class EnemyTank {
    //执行速度
    private static final int SPEED = 10;
    private Logger logger = LoggerFactory.getLogger(EnemyTank.class);
    //坦克的X，Y轴
    private int x, y;
    //坦克的方向
    private Dir dir;
    //坦克的类型（敌人，友军）
    private TankGroup group;
    //坦克是否移动属性
    private boolean moving = true;
    //是否存活
    private boolean isLive = true;
    //移动的方向
    private boolean isL, isU, isR, isD;
    //记录敌人的上一步的位置
    private int oldX, oldY;
    //坦克的宽高
    private int width, height;
    //导弹发射
    private Random rFire = new Random();
    /**
     * 随机移动
     */
    private Random r = new Random();

    public EnemyTank(int x, int y, Dir dir, TankGroup group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;
        this.oldX = x;
        this.oldY = y;
        this.width = ResourseMgr.enemyTankU.getWidth();
        this.height = ResourseMgr.enemyTankU.getHeight();
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public boolean isLive() {
        return isLive;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    //绘制方块
    public void paint(Graphics g) {
        //logger.info("测试info");
        //logger.error("测试error");
        //logger.debug("测试debug");
        if (!this.isLive) return;

        switch (dir) {
            case UP:
                g.drawImage(ResourseMgr.enemyTankU, x, y, null);
                break;
            case LEFT:
                g.drawImage(ResourseMgr.enemyTankL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(ResourseMgr.enemyTankR, x, y, null);
                break;
            case DOWN:
                g.drawImage(ResourseMgr.enemyTankD, x, y, null);
                break;
        }

        move();
    }

    private void tankFire() {
        if (!this.isLive) return;
        if (rFire.nextInt(100) > 90) {
            int mx = x + ResourseMgr.enemyTankU.getWidth() / 2 - ResourseMgr.enemyMissile.getWidth() / 2;
            int my = y + ResourseMgr.enemyTankU.getHeight() / 2 - ResourseMgr.enemyMissile.getHeight() / 2;
            TankFrame.getInstance().addMissile(new Missile(mx, my, this.dir, this.group));
        }

    }

    //设置方向的移动
    private void move() {
        if (!moving) return;

        oldX = x;
        oldY = y;

        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }
        borderLineChk();
        randomMove();
        tankFire();
    }

    //设置坦克是否到达边界
    private void borderLineChk() {
        if (x < 0 || y < 30 || x + width > TankFrame.GAME_W || y + height > TankFrame.GAME_H) {
            this.tankBack();
        }
    }

    private void tankBack() {
        this.x = oldX;
        this.y = oldY;
    }

    private void randomMove() {
        if (r.nextInt(100) > 90) {
            this.dir = Dir.randomDir();
        }
    }

    /**
     * 当坦克死亡则设置存活属性为死亡
     */
    public void die() {
        this.setLive(false);
        new Thread(()->new Audio("audio/blast.wav").play()).start();
        TankFrame.getInstance().addBlast(new TankBlast(x,y));
    }


}
